import 'dart:developer';
import 'dart:math';

import 'package:fl_chart/fl_chart.dart';
import 'package:flutter/material.dart';
import 'package:smart_freezer/core/utils/logs.dart';

class LCLineChart extends StatelessWidget {

  List<String> x1List;
  List<double> y1List;

  List<String> x2List;
  List<double> y2List;

  LCLineChart({required this.x1List, required this.y1List, required this.x2List, required this.y2List});

  @override
  Widget build(BuildContext context) {
    return Container(
      // color: Colors.red,
      child: LineChart(
        getSampleData1(),
        swapAnimationDuration: const Duration(milliseconds: 250),
      ),
    );
  }

  // T1 温度
  List<Color> gradientColors_1 = [
    const Color(0xFFFF2366),
    const Color(0xFF8D4DE8),
  ];
  // T2 温度
  List<Color> gradientColors_2 = [
    const Color(0xFF57AFBB),
    const Color(0xFF5282F2),
  ];


  LineChartData getSampleData1() {
    var y1Max = y1List.reduce(max);
    var y2Max = y2List.reduce(max);
    var y1Min = y1List.reduce(min);
    var y2Min = y2List.reduce(min);
    var maxResult = [y1Max, y2Max, y1Min, y2Min].reduce(max);
    var minResult = [y1Max, y2Max, y1Min, y2Min].reduce(min);

    // logs("最大值: ${maxResult} ${minResult}");

    return LineChartData(
      lineTouchData: lineTouchData1,
      gridData: gridData,
      titlesData: titlesData1,
      borderData: borderData,
      lineBarsData: lineBarsData1,
      minX: 0,
      maxX: x1List.length * 1.0,
      maxY: maxResult + 2,
      minY: minResult - 2,
    );
  }

  LineTouchData get lineTouchData1 => LineTouchData(
    handleBuiltInTouches: true,
    touchTooltipData: LineTouchTooltipData(
      tooltipBgColor: Colors.blueGrey.withOpacity(0.8),
    ),
  );

  FlTitlesData get titlesData1 => FlTitlesData(
    bottomTitles: AxisTitles(
      sideTitles: bottomTitles,
    ),
    rightTitles: AxisTitles(
      sideTitles: SideTitles(showTitles: false),
    ),
    topTitles: AxisTitles(
      sideTitles: SideTitles(showTitles: false),
    ),
    leftTitles: AxisTitles(
      sideTitles: leftTitles(),
    ),
  );

  List<LineChartBarData> get lineBarsData1 => [
    getLineChartBarData1_1(),
    getLineChartBarData1_2(),
  ];

  LineTouchData get lineTouchData2 => LineTouchData(
    enabled: false,
  );

  FlTitlesData get titlesData2 => FlTitlesData(
    bottomTitles: AxisTitles(
      sideTitles: bottomTitles,
    ),
    rightTitles: AxisTitles(
      sideTitles: SideTitles(showTitles: false),
    ),
    topTitles: AxisTitles(
      sideTitles: SideTitles(showTitles: false),
    ),
    leftTitles: AxisTitles(
      sideTitles: leftTitles(),
    ),
  );

  Widget leftTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      color: Color(0xff75729e),
      fontWeight: FontWeight.bold,
      fontSize: 14,
    );
    String text = "";
    // logs("leftValue: $value");

    if(value == meta.max) {
      text = "${value}℃";
    }else if(value == meta.min) {
      text = "${value}℃";
    }else if((meta.max + meta.min) / 2 == value) {
      text = "${value}℃";
    }

    return Text(text, style: style, textAlign: TextAlign.center);
  }

  SideTitles leftTitles() => SideTitles(
    getTitlesWidget: leftTitleWidgets,
    showTitles: true,
    interval: 1,
    reservedSize: 40,
  );

  Widget bottomTitleWidgets(double value, TitleMeta meta) {
    const style = TextStyle(
      color: Color(0xff72719b),
      // fontWeight: FontWeight.bold,
      fontSize: 13,
    );
    String text = "";

    // logs("图表数据: 最大值${meta.max} 最小值${meta.min} ${meta.appliedInterval} ${meta.formattedValue} => $value");
    if(value == meta.max) {
      text = "${x1List[x1List.length - 1]}";
    }else if(value == meta.min) {
      text = "${x1List[0]}";
    }else if((meta.max + meta.min) / 2 == value) {
      final midValue = value.toInt();
      text = "${x1List[midValue]}";
    }

    return Container(
      child: Text(text, style: style),
      padding: EdgeInsets.only(top: 5.0, bottom: 1, left: value == meta.min ? 30 : 0, right: value == meta.max ? 30 : 0),
    );

  }

  SideTitles get bottomTitles => SideTitles(
    showTitles: true,
    reservedSize: 22, // x1List.length * 1.0,
    interval: 1,
    getTitlesWidget: bottomTitleWidgets,
  );

  FlGridData get gridData => FlGridData(show: false);

  FlBorderData get borderData => FlBorderData(
    show: true,
    border: const Border(
      bottom: BorderSide(color: Color(0xff4e4965), width: 2),
      left: BorderSide(color: Colors.transparent),
      right: BorderSide(color: Colors.transparent),
      top: BorderSide(color: Colors.transparent),
    ),
  );

  LineChartBarData getLineChartBarData1_1() {
    var spotList = x1List.map((e) {
      final index = x1List.indexOf(e);
      final yValue = index < y1List.length ? y1List[index] : 0.0;
      return FlSpot(index * 1.0, yValue);
    }).toList();

    // spotList = const [
    //   FlSpot(1, 1 + 1),
    //   FlSpot(3, 1.5 + 1),
    //   FlSpot(5, 1.4 + 1),
    //   FlSpot(7, 3.4 + 1),
    //   FlSpot(10, 2 + 1),
    //   FlSpot(12, 2.2 + 1),
    //   FlSpot(13, 1.8 + 1),
    // ];

    return LineChartBarData(
      isCurved: true,
      color: const Color(0xFFFF2366),
      barWidth: 3,
      isStrokeCapRound: true,
      dotData: FlDotData(show: false),
      belowBarData: BarAreaData(
          show: true,
          gradient: LinearGradient(
            colors: gradientColors_1
                .map((color) => color.withOpacity(0.3))
                .toList(),
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
          )
      ),  // 显示区间颜色
      spots: spotList,

      // 线条渐变色
      gradient: LinearGradient(
        colors: gradientColors_1,
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
    );
  }

  LineChartBarData getLineChartBarData1_2() {
    var spotList = x2List.map((e) {
      final index = x2List.indexOf(e);
      final yValue = index < y2List.length ? y2List[index] : 0.0;
      return FlSpot(index * 1.0, yValue);
    }).toList();

    // spotList = const [
    //   FlSpot(1, 1),
    //   FlSpot(3, 1.5),
    //   FlSpot(5, 1.4),
    //   FlSpot(7, 3.4),
    //   FlSpot(10, 2),
    //   FlSpot(12, 2.2),
    //   FlSpot(13, 1.8),
    // ];

    return LineChartBarData(
      isCurved: true,
      color: const Color(0xFF57AFBB),
      barWidth: 3,
      isStrokeCapRound: true,
      dotData: FlDotData(show: false),
      belowBarData: BarAreaData(
          show: true,
          gradient: LinearGradient(
            colors: gradientColors_2
                .map((color) => color.withOpacity(0.3))
                .toList(),
            begin: Alignment.centerLeft,
            end: Alignment.centerRight,
          )
      ),  // 显示区间颜色
      spots: spotList,

      // 线条渐变色
      gradient: LinearGradient(
        colors: gradientColors_2,
        begin: Alignment.topCenter,
        end: Alignment.bottomCenter,
      ),
    );
  }


}

